﻿package mortal.game.scene3D.model.player
{
    import Message.DB.Tables.*;
    import baseEngine.basic.*;
    import baseEngine.modifiers.*;
    import baseEngine.system.*;
    import com.gengine.global.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.effectEditTool.temple.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import frEngine.render.layer.uiEffectLayer.*;
    import mortal.game.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.task.data.*;

    public class WizardScreenPlayer extends Object
    {
        private var _timeId:int = -1;
        private var _bodyPlayer:WizardActionPlayer;
        private var _timeKey:int;
        private var timerContorler:TimeControler;
        private var _showTween:TweenMax;
        private var _showTween2:TweenMax;
        private var _hasLoaded:Boolean = false;
        private var _effectPlayer:EffectPlayer;
        private var _effectUrl:String;
        private var renderList:RenderList;
        public static const instance:WizardScreenPlayer = new WizardScreenPlayer;

        public function WizardScreenPlayer()
        {
            this.renderList = UIEffectList.instance.getRenderListByUILayerID(UILayerIDs.ScreenPlayer);
            var _loc_1:* = Global3D;
            _loc_1.timeKeyId = Global3D.timeKeyId + 1;
            this._timeKey = Global3D.timeKeyId + 1;
            this.timerContorler = TimeControler.createTimerInstance(null, null, this._timeKey);
            this.timerContorler.unActive(this._timeKey);
            return;
        }// end function

        private function getEffectPlayer(param1:String) : EffectPlayer
        {
            if (this._effectPlayer && this._effectPlayer.effectUrl != param1)
            {
                this._effectPlayer.dispose();
                this._effectPlayer = null;
            }
            if (!this._effectPlayer || this._effectPlayer.isDisposed || this._effectPlayer.isInPool)
            {
                this._effectPlayer = EffectPlayerPool.instance.getEffectPlayer(param1, null, this.renderList, false, true);
                if (this._effectPlayer.temple is TempleRole)
                {
                    (this._effectPlayer.temple as TempleRole).setRoleParams(this._bodyPlayer, null, null);
                }
            }
            this._effectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
            return this._effectPlayer;
        }// end function

        public function play(param1:String) : void
        {
            var _loc_2:* = ModelConfig.instance.getInfoByCode(int(param1));
            this._effectUrl = _loc_2.effect1;
            if (this._bodyPlayer == null || this._bodyPlayer.isDisposed)
            {
                this._bodyPlayer = FrObjectPool.getObject(WizardActionPlayer, this);
                this._bodyPlayer.timerContorler = this.timerContorler;
                FrEventDispatcher.instance.proxyAddEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
            }
            var _loc_3:* = _loc_2.mesh1 + ".md5mesh";
            var _loc_4:* = _loc_2.bone1 + ".skeleton";
            this._bodyPlayer.load(_loc_3, _loc_4, _loc_2.texture1, this.renderList);
            if (_loc_2.mesh2)
            {
                _loc_3 = _loc_2.mesh2 + ".md5mesh";
                this._bodyPlayer.loadExtend(_loc_3, _loc_4, _loc_2.texture1, this.renderList);
                this._bodyPlayer.extendPlayer.timerContorler = this.timerContorler;
            }
            else
            {
                this._bodyPlayer.clearExtend();
            }
            if (this._hasLoaded)
            {
                this.toplay();
            }
            return;
        }// end function

        private function onInitBodyPlayer(event:Event) : void
        {
            this._hasLoaded = true;
            this.toplay();
            return;
        }// end function

        private function toplay() : void
        {
            if (this._timeId > 0)
            {
                clearTimeout(this._timeId);
            }
            this._timeId = setTimeout(this.playImp, 500);
            return;
        }// end function

        private function playImp() : void
        {
            this._timeId = -1;
            if (RolePlayer.instance.wizardSmallPlayer)
            {
                RolePlayer.instance.wizardSmallPlayer.visible = false;
            }
            this._bodyPlayer.play();
            this.timerContorler.active(this._timeKey);
            this._bodyPlayer.scaleValue = 0.5;
            this._bodyPlayer.alpha = 0.2;
            this._bodyPlayer.changeAction(ActionName.Standing);
            this._bodyPlayer.timerContorler.gotoFrame(0, 0);
            this._bodyPlayer.addFrameComplete(ActionName.Standing, this.switchActionHander);
            this._bodyPlayer.addChild(this.getEffectPlayer(this._effectUrl));
            var _loc_1:* = (-Global.stage.stageWidth) / 8 - 100;
            this._bodyPlayer.y = _loc_1;
            this._showTween = TweenMax.to(this._bodyPlayer, 2, {y:_loc_1 + 100, alpha:1, scaleValue:3.2, ease:Quint.easeOut, frameInterval:1});
            this._bodyPlayer.parent = Game.scene.playerLayer;
            return;
        }// end function

        private function switchActionHander() : void
        {
            this._bodyPlayer.changeAction(ActionName.Stand);
            this._bodyPlayer.timerContorler.gotoFrame(0, 0);
            if (this._timeId != -1)
            {
                clearTimeout(this._timeId);
            }
            this._timeId = setTimeout(this.tohide, 100);
            return;
        }// end function

        private function tohide() : void
        {
            this._timeId = -1;
            this._effectPlayer.dispose();
            this._effectPlayer = null;
            this._showTween.kill();
            this._bodyPlayer.alpha = 0.5;
            var _loc_1:* = (-Global.stage.stageWidth) / 8 - 100;
            this._showTween = TweenMax.to(this._bodyPlayer, 3.5, {y:_loc_1, alpha:0, scaleValue:0.2, ease:Quint.easeOut, frameInterval:1, onComplete:this.toClear});
            return;
        }// end function

        private function toClear() : void
        {
            this._showTween.kill();
            if (RolePlayer.instance.wizardSmallPlayer)
            {
                RolePlayer.instance.wizardSmallPlayer.visible = true;
            }
            this._bodyPlayer.stop();
            this.timerContorler.unActive(this._timeKey);
            this._bodyPlayer.parent = null;
            return;
        }// end function

    }
}
